/*
* @Author: jeffrey
* @Date:   2017-09-13 10:09:07
* @Last Modified by:   mz_zml
* @Last Modified time: 2018-06-12 22:21:12
*/
var gulp = require('gulp'),
    concat = require('gulp-concat'),
    minifyCss = require('gulp-minify-css'),
    rev = require('gulp-rev'),
    revCollector = require('gulp-rev-collector'),
    rename = require('gulp-rename'),
    uglify = require('gulp-uglify'),
    useref = require('gulp-useref'),
    connect = require('gulp-connect'),
    imagemin = require('gulp-imagemin'),
    del = require('del'),
    gulpsync = require('gulp-sync')(gulp),
    browserSync = require('browser-sync'),
    reload = browserSync.reload,
    htmlmin = require('gulp-htmlmin'),
    less = require('gulp-less'),
    stylus = require('gulp-stylus'),
    babel = require('gulp-babel')
 

gulp.task('style', function () {
  del.sync(['dist/css'])
  return gulp.src(['src/css/*.styl'])
          .pipe(stylus())
          // .pipe(concat('index.css'))
          .pipe(gulp.dest('src/css'))
          .pipe(minifyCss())
          .pipe(rename(function (path) {
            // path.basename = path.basename.search(/.min$/gi) == -1 ? path.basename + '.min' : path.basename
            path.extname = '.css'
          }))
          .pipe(rev()) 
          .pipe(gulp.dest('dist/css'))                          
          .pipe(rev.manifest({
            path: 'rev-manifest-css.json'
          }))                                
          .pipe(gulp.dest('dist/rev/css')) 
          .pipe(connect.reload()) 
})

gulp.task('script', function() {
  del.sync(['dist/js'])
  return gulp.src('src/js/*.js')
          // .pipe(babel())
          .pipe(uglify())
          .pipe(rev())
          .pipe(rename(function (path) {
            // path.basename = path.basename.search(/.min$/gi) == -1 ? path.basename + '.min' : path.basename
            path.extname = '.js'
          }))
          .pipe(gulp.dest('dist/js'))
          .pipe(rev.manifest({
            path: 'rev-manifest-js.json'
          })) 
          .pipe(gulp.dest('dist/rev/js'))
          .pipe(connect.reload())
})

gulp.task('images', function () {
  return gulp.src('src/img/**/*.*')
          .pipe(imagemin())
          .pipe(gulp.dest('dist/img'))
          .pipe(connect.reload())
})

gulp.task('rev', function() {
  var options = {
        removeComments: true,
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        removeEmptyAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        minifyJS: true,
        minifyCSS: true
    }
  return  gulp.src(['dist/rev/**/*.json', 'src/*.html']) 
            .pipe(useref())
            .pipe(htmlmin(options))
            .pipe(revCollector({
              replaceReved: true
            }))
            .pipe(gulp.dest('dist/'))
            .pipe(connect.reload())
})

gulp.task('watch', function () {
  gulp.watch('src/css/*.styl', ['style'])
  gulp.watch('src/js/*.js', ['script'])
  gulp.watch('src/img/*.*', ['images'])
  gulp.watch('src/*.html', ['rev'])
  gulp.watch('dist/rev/**/*.*', ['rev'])
})

gulp.task('server', function() {
  browserSync({
    server: {
      baseDir: 'src/',
　　　 tunnel: true
    },
    open: 'external',
    injectChanges: true
  })

  // gulp.watch(['src/*.html', 'src/css/*.css', 'src/js/*.js', 'src/img/**/*.*'], {
  //   cwd: 'src/'
  // }, reload)
})

gulp.task('connect', function() {
  connect.server({
    root: 'src/',
    livereload: true
  })
})

gulp.task('clean', function () {
    del.sync(['dist/css', 'dist/images', 'dist/js', 'dist/*.html', 'dist/rev'])
})

gulp.task('quick', gulpsync.sync(['clean', ['style', 'script', 'images'], 'rev', 'watch']))

gulp.task('default', gulpsync.sync(['clean', ['style', 'script', 'images'], 'rev', 'watch', 'server']))